<?php

/**
 * @author kevin
 * @date 2016-01-16 14:00:00
 * @desc 促销活动
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$version = '1.0';   //版本号

if ($_POST['api_version'] != $version)      //网店的接口版本低
{
	api_err('0x008', 'a low version api');
}

//促销活动列表
if($_POST['st'] == 'list'){
	$data = getads(6,10);
}
//活动详情
if($_POST['st'] == 'detail'){    
    $topic_id  = empty($_POST['topic_id']) ? 0 : abs(intval($_POST['topic_id']));
    if(empty($topic_id)){
        api_err('0x008','param topic_id can not empty');
    }
    $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('topic') ."WHERE topic_id = '$topic_id' and  " . gmtime() . " >= start_time and " . gmtime() . "<= end_time";
    $topic = $GLOBALS['db']->getRow($sql);
    if(empty($topic)){
        api_err('0x008', 'topic_id is not exists');
    }
    $topic['data'] = addcslashes($topic['data'], "'");
    $tmp = @unserialize($topic["data"]);
    $arr = (array)$tmp;

    $goods_id = array();

    foreach ($arr AS $key=>$value)
    {
        foreach($value AS $k => $val)
        {
            $opt = explode('|', $val);
            $arr[$key][$k] = $opt[1];
            $goods_id[] = $opt[1];
        }
    }

    $sql = 'SELECT g.goods_id, g.goods_name, g.market_price, g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price,g.goods_number, ' .
                "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, " .
                'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img ' .
                'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
                'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . ' AS mp ' .
                "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .
                "WHERE " . db_create_in($goods_id, 'g.goods_id');

    $res = $GLOBALS['db']->query($sql);

    $sort_goods_arr = array();

    while ($row = $GLOBALS['db']->fetchRow($res)){
        unset($row['org_price'], $row['promote_end_date'], $row['promote_start_date']);
        unset($row['is_best'], $row['is_hot'], $row['is_new']);
        $row['goods_thumb']      = get_image_pathv1($row['goods_id'], $row['goods_thumb'], true);
        $row['goods_img']      = get_image_pathv1($row['goods_id'], $row['goods_img'], true);
	    $row['watermark_img'] =  get_water_img($row['goods_id'],$row['is_new'],$row['promote_price']);
        $sort_goods_arr[] = $row;
    }
    $data = array(
        'topic_img' => get_image_pathv1('',$topic['topic_img']),
        'goods_list' => $sort_goods_arr,
    );
}
//st参数错误
if(!isset($data)){
	api_err('0x008');
}
//返回数据
data_back($data, '', RETURN_TYPE);
